﻿using System;
using System.Windows;
using Baosight.Platform.Core;
using Baosight.Platform.Core.UI.Controls;
using Baosight.ZNH.ZMCommon;
using Baosight.ZNH.ZMCommon.ChildWindows;


namespace Baosight.ZNH.ZMCM.UI.ChildWindows
{
    public partial class MaterialBaseHis : EFViewCmes
    {
        public MaterialBaseHis(string dialogType)
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
            Init(dialogType);      
        }

        /// <summary>
        /// 初始化父类EFViewCmesProperty的值，每次点击“确定”、“返回”时执行此方法，从新赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.spWindows.GetChildObjects<EFDataField>(null, true);
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 初始化页面数据
        /// </summary>
        private void Init(string type)
        {
            type = type == null ? "insert" : type.ToLower();

            switch (type)
            {
                case "jiezhuan":
                    base.Mode = OperationMode.Insert;
                    //this.fdHisDate.IsEnabled = false;//结转日期不可编辑
                    break;
                case "detail":
                    this.Commit.Visibility = Visibility.Collapsed;
                    BaseTools.EFDataFieldNotEnabled(this.spWindows.GetChildObjects<EFDataField>(null, true));
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitClick(object sender, RoutedEventArgs e)
        {
            base.OnCommitButtonClick("ZMCM91", sender, e);
        }

        /// <summary>
        /// 确定前的结转日期校验
        /// </summary>
        /// <returns></returns>
        public override bool ThisValidateField()
        {
            if (this.fdHisDate.Value != null)
            {
                string[] ApplicationDate = this.fdHisDate.Value.ToString().Split('-');
                //if (validateDate(ApplicationDate) == false)
                //{
                //    return false;
                //}
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 保证日期在计划月份内
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        private bool validateDate(string[] date)
        {
            if (int.Parse(this.fdYear.Value.ToString()) != int.Parse(date[0]))
            {
                base.OpenEFWindowAlert("所选择的结转年份不是当前年份，请重新选择年份！", true);
                return false;
            }
            if (int.Parse(this.fdMonth.Value.ToString()) < int.Parse(date[1]))
            {
                base.OpenEFWindowAlert("所选择的结转月份不能小于当前月份，请重新选择月份！", true);
                return false;
            }
            return true;
        }

        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelClick(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }
    }
}
